/**
 * Created by 준영 on 2014-05-12.
 */

var async = require('async');
var util = require("util");

var BaseApi = require('./baseApi').BaseApi;

exports.AccountApi = (function() {
    function AccountApi(mongoDB, keyValueStore, sessionStore) {
        this.mongoDB = mongoDB;
        this.keyValueStore = keyValueStore;
        this.sessionStore = sessionStore;
    }

    util.inherits(AccountApi, BaseApi);


    AccountApi.method('logout', function(req, res)
    {
        req.session.destroy(function(err){

            req.session = null;
            res.redirect('/');
        });

    });

    AccountApi.method("CheckAuth", function(req, res)
    {
        var self = this;
        self.mongoDB.db.collection('users').findOne({ "id": req.query.id, "passwd" : req.query.passwd },function(err, user)
        {
            if(user != null)
            {
                self.mongoDB.db.collection('users').findOne({"id":req.query.id },function(findOneError, findOneResult) {
                    if (findOneError == null) {

                        req.session.user = req.query.id;
                        req.session.userInfo = findOneResult;
                        //  res.send("success");
                        var url = self.keyValueStore.getAuthRedirectionUrl(req.connection.remoteAddress, function (err, result) {
                            if (result !== null) {
                                self.keyValueStore.removeAuthRedirectionUrl(req.connection.remoteAddress);
                                res.send({ "Error": "", "RedirectionUrl": result});
                            }
                            else {
                                res.send({ "Error": "", "RedirectionUrl": '/'});
                            }
                        });
                    }
                });


            }
            else
            {
                res.send({ "Error": "ID 또는 비밀번호가 잘못 되었습니다.", "RedirectionUrl": ''});
            }
        });
    });

    AccountApi.method("alreadyUserCheck", function(req, res)
    {
        var self = this;
        var isAlreadyId ;
        var isAlreadyNickName;


        var data = req.query;
        async.waterfall([
            function (callback) {
                self.mongoDB.db.collection('users').findOne({"id": data.id}, function(err, user)
                {

                    isAlreadyId = user;
                    callback(null);
                });

            },
            function (callback){
                self.mongoDB.db.collection('users').findOne({"nickname": data.nickname},function(err, user)
                {
                    isAlreadyNickName = user;
                    callback(null);
                });


            }
        ], function(err){

            if(isAlreadyId !== null) {
                res.send("이미 있는 ID입니다.");
            }
            else if(isAlreadyNickName !== null)
            {
                res.send("이미 있는 닉네임 입니다.");
            }
            else
            {
                res.send("success");
            }
        });



    });

    AccountApi.method("createUser", function(req, res)
    {

        this.mongoDB.db.collection('users').insert(req.body, function (err, result) {

        });
    });




    return AccountApi;
})();